<template>
  <div class="app-container">
    <div class="search-container">
      <el-form inline>
        <el-form-item label="名称">
          <el-input />
        </el-form-item>
        <el-button type="primary">搜索</el-button>
        <el-button type="success" @click="operate('add')">新增</el-button>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号" width="65">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="用户姓名">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scope">
          {{ scope.row.logo }}
        </template>
      </el-table-column>
      <el-table-column label="性别" width="80">
        <template slot-scope="scope">
          {{ scope.row.sex }}
        </template>
      </el-table-column>
      <el-table-column label="联系电话" width="120">
        <template slot-scope="scope">
          {{ scope.row.tel }}
        </template>
      </el-table-column>
      <el-table-column label="角色" width="120">
        <template slot-scope="scope">
          {{ scope.row.role }}
        </template>
      </el-table-column>
      <el-table-column label="所属部门">
        <template slot-scope="scope">
          {{ scope.row.dept }}
        </template>
      </el-table-column>
      <el-table-column label="职位" width="100">
        <template slot-scope="scope">
          {{ scope.row.position }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180">
        <template slot-scope="scope">
          {{ scope.row.time }}
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status"></el-switch>
        </template>
      </el-table-column>

      <!-- <el-table-column class-name="status-col" label="是否在职" width="110" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status | statusFilterLabel }}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="180" align="center" fixed="right">
        <template slot-scope="scope">
          <!-- <el-button type="primary" size="mini" :disabled="scope.row.status == 0">发布</el-button> -->
          <!-- <el-button type="warning" size="mini" :disabled="scope.row.status == 1">停用</el-button> -->
          <el-button
            type="success"
            size="mini"
            @click="operate('edit', scope.row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" :title="title" width="500px">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.role" placeholder="请选择角色">
            <el-option
              v-for="(item, index) in roleList"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门">
          <el-select v-model="form.dept" placeholder="请选择部门">
            <el-option
              v-for="(item, index) in deptList"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="职位">
          <el-select v-model="form.num" placeholder="请选择职位">
            <el-option
              v-for="(item, index) in numList"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.delivery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from "@/api/table";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: "success",
        1: "info"
      };
      return statusMap[status];
    },
    statusFilterLabel(status) {
      const statusMap = {
        0: "在职",
        1: "离职"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      data: [
        {
          name: "张三",
          logo: "",
          sex: "女",
          tel: "13788890000",
          role: "项目负责人",
          dept: "项目部",
          position: "部门经理",
          time: "2023-12-24 16:10:10",
          status: false
        },
        {
          name: "李四",
          logo: "",
          sex: "男",
          tel: "13788890000",
          role: "评估员",
          dept: "管理部",
          position: "员工",
          time: "2023-12-24 16:10:10",
          status: true
        },
        {
          name: "王五",
          logo: "",
          sex: "男",
          tel: "13788890000",
          role: "施工人员",
          dept: "服务部",
          position: "员工",
          time: "2023-12-24 16:10:10",
          status: true
        }
      ],
      dialogVisible: false,
      title: "新增类目",
      deptList: [
        { label: "项目部", value: "0" },
        { label: "管理部", value: "1" },
        { label: "服务部", value: "2" },
        { label: "施工部", value: "3" },
        { label: "设计部", value: "4" }
      ],
      numList: [
        { label: "部门经理", value: "0" },
        { label: "员工", value: "1" }
      ],
      typeList: [
        { label: "消防安全", value: "0" },
        { label: "食堂安全", value: "1" },
        { label: "护工查房", value: "2" }
      ],
      roleList: [
        { label: "项目负责人", value: "0" },
        { label: "评估员", value: "1" },
        { label: "施工人员", value: "2" },
        { label: "设计人员", value: "3" },
        { label: "售后人员", value: "4" },
        { label: "验收人员", value: "5" },
        { label: "管理员", value: "6" }
      ],
      form: {}
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      this.list = this.data;
      this.listLoading = false;
      // getList().then(response => {
      //   this.list = response.data.items
      //   this.listLoading = false
      // })
    },
    operate(type, row) {
      if (type === "add") {
        this.dialogVisible = true;
        this.title = "新增用户";
      } else if (type === "edit") {
        this.dialogVisible = true;
        this.title = "修改用户";
        this.form = JSON.parse(JSON.stringify(row));
      }
    },
    onSubmit() {
      this.$message("submit!");
    },
    onCancel() {
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped>
.line {
  text-align: center;
}
</style>
